/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.common.util;

import org.osgeo.proj4j.CRSFactory;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import org.osgeo.proj4j.CoordinateTransform;
import org.osgeo.proj4j.CoordinateTransformFactory;
import org.osgeo.proj4j.ProjCoordinate;

/**
 * 经纬度，墨卡托等坐标转换
 *
 * @author duanlikao
 * @created 2021/1/17
 */
public class Proj4jUtil {
    static final String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
    CoordinateReferenceSystem WGS84 = crsFactory.createFromParameters("WGS84",
            WGS84_PARAM);

    private static final CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
    private static final CRSFactory crsFactory = new CRSFactory();

    private static CoordinateReferenceSystem createCRS(String crsSpec) {
        CoordinateReferenceSystem crs = null;
        // test if name is a PROJ4 spec
        if (crsSpec.indexOf("+") >= 0 || crsSpec.indexOf("=") >= 0) {
            crs = crsFactory.createFromParameters("Anon", crsSpec);
        } else {
            crs = crsFactory.createFromName(crsSpec);
        }
        // crs = crsFactory.createFromParameters("Anon", crsSpec);

        return crs;
    }

    private static void lonlat2m() {
        System.out.println("....");

        double x1 = 114.549371;
        double y1 = 37.294329;
        String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
        String tgtCRS = "+proj=tmerc +lat_0=0 +lon_0=117 +y_0=0 +x_0=500000 +k=0.9996 +zone=50 +to_meter=1 +a=6378137 +ellps=WGS84 +units=m +no_defs";

        CoordinateTransform trans = ctFactory
                .createTransform(createCRS(WGS84_PARAM), createCRS(tgtCRS));
        ProjCoordinate pout = new ProjCoordinate();
        ProjCoordinate p = new ProjCoordinate(x1, y1);
        trans.transform(p, pout);

        System.out.println(p.x);
        System.out.println(p.y);
        System.out.println(pout.x);
        System.out.println(pout.y);

        System.out.println("-------		// 117.19625 31.83879 518568.9 3522583.9 ");
        p = new ProjCoordinate(y1, x1);
        trans.transform(p, pout);

        //		System.out.println(p.x);
        //		System.out.println(p.y);
        //		System.out.println(pout.x);
        //		System.out.println(pout.y);
    }

    public static void main(String[] args) {
        lonlat2m();
    }



    public static double[] m2lonlat(double x1,double y1) {
        String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
        String tgtCRS = "+proj=tmerc +lat_0=0 +lon_0=117 +y_0=0 +x_0=500000 +k=0.9996 +zone=50 +to_meter=1 +a=6378137 +ellps=WGS84 +units=m +no_defs";
        CoordinateTransform trans = ctFactory
                .createTransform( createCRS(tgtCRS),createCRS(WGS84_PARAM));
        ProjCoordinate pout = new ProjCoordinate();
        ProjCoordinate p = new ProjCoordinate(x1, y1);
        trans.transform(p, pout);
        double[] xy = new double[2];
        xy[0] = pout.x;
        xy[1] = pout.y;
        return xy;
    }
}